package com.jinzhi.jzweb.controller.moduleConfig;


import java.util.*;
import java.util.stream.Collectors;

import com.jinzhi.api.exception.IFastApiException;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.domain.moduleConfig.TrackDrawDO;
import com.jinzhi.jzweb.domain.moduleConfig.TrackDrawDownLoadDO;
import com.jinzhi.jzweb.pojo.moduleConfig.CareStuSignUpComRealityDownloadVo;
import com.jinzhi.jzweb.pojo.moduleConfig.StuDecryptVo;
import com.jinzhi.jzweb.service.moduleConfig.TrackDrawService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * <pre>
 * 赛道抽签表（存储抽签信息）
 * </pre>
 * <small> 2024-02-28 09:32:18 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/trackDraw")
public class TrackDrawController extends BaseController {
	@Autowired
	private TrackDrawService trackDrawService;

	@GetMapping()
	@RequiresPermissions("jzweb:trackDraw:list")
	String TrackDraw(){
	    return "jzweb/trackDraw/trackDraw";
	}

	/**
	 * 抽签接口
	 * @param trackDrawDTO
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	public Result<List<TrackDrawDO>> list(TrackDrawDO trackDrawDTO){

		return trackDrawService.trackDrawList(trackDrawDTO);
	}

	/**
	 * 查询上次的抽签结果
	 */
	@ResponseBody
	@GetMapping("/trackList")
	public Result<List<TrackDrawDO>> trackList(TrackDrawDO trackDrawDTO){

		return Result.ok(trackDrawService.trackList(trackDrawDTO));
	}

	/**
	 * 导出抽签结果信息
	 *
	 * @param response
	 */
	@Log("导出抽签结果信息")
	@RequestMapping("/downloadImportTemplate")
	@ResponseBody
	public void downloadImportTemplate(HttpServletRequest request, HttpServletResponse response, TrackDrawDO trackDrawDTO) {
		// 先校验是否进行了抽签
		Wrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
		//wrapper.orderBy(true,"record_number");
		wrapper.orderBy(true,"record_number_sort");
		// 查询数据库中的抽签表
		List<TrackDrawDO> trackDrawDOS = trackDrawService.selectList(wrapper);
		// 将查询出的数据转换成 4*18
		List<TrackDrawDO> setTemplateTrackDrawDOSList = trackDrawService.setTemplate(trackDrawDOS);
		// 导出集合
		List<TrackDrawDownLoadDO> trackDrawDownLoadDOS = new ArrayList<>();
		if (setTemplateTrackDrawDOSList != null && setTemplateTrackDrawDOSList.size() > 0) {
			// 循环遍历 转换数据
			for (TrackDrawDO parentTrackDraw : setTemplateTrackDrawDOSList) {
				if (parentTrackDraw.getTrackDrawDOList() != null && parentTrackDraw.getTrackDrawDOList().size() > 0) {
					// 导出对象
					TrackDrawDownLoadDO trackDrawDownLoadDO = new TrackDrawDownLoadDO();
					// 每行的数据量
					int listSize = parentTrackDraw.getTrackDrawDOList().size();
					// 第一列数据
					trackDrawDownLoadDO.setUnitName(listSize > 0 ? parentTrackDraw.getTrackDrawDOList().get(0).getUnitName() : "");
					trackDrawDownLoadDO.setRecordNumber(listSize > 0 ? parentTrackDraw.getTrackDrawDOList().get(0).getRecordNumber() : "");
					// 判断一行数据是否是两个
					if (listSize > 1) {
						trackDrawDownLoadDO.setUnitName2(parentTrackDraw.getTrackDrawDOList().get(1).getUnitName());
						trackDrawDownLoadDO.setRecordNumber2(parentTrackDraw.getTrackDrawDOList().get(1).getRecordNumber());
					}
					// 判断一行数据是否是三个
					if (listSize > 2) {
						trackDrawDownLoadDO.setUnitName3(parentTrackDraw.getTrackDrawDOList().get(2).getUnitName());
						trackDrawDownLoadDO.setRecordNumber3(parentTrackDraw.getTrackDrawDOList().get(2).getRecordNumber());
					}
					// 判断一行数据是否是四个
					if (listSize > 3) {
						trackDrawDownLoadDO.setUnitName4(parentTrackDraw.getTrackDrawDOList().get(3).getUnitName());
						trackDrawDownLoadDO.setRecordNumber4(parentTrackDraw.getTrackDrawDOList().get(3).getRecordNumber());
					}
					// 添加到导出集合
					trackDrawDownLoadDOS.add(trackDrawDownLoadDO);
				}
			}
				//导出操作
				EasyPoiUtil.exportExcel(trackDrawDownLoadDOS, "领队抽签信息导出", "领队抽签信息导出", TrackDrawDownLoadDO.class,  "领队抽签信息导出", response);
		} else {
			throw new IFastApiException(ApiEnumError.NOTBATCHSTU.getCodeStr());
		}
	}
}
